<?php

class Service_Resource
{
    const MAX_ITEMS_COUNT = 1000;
    const DEFAULT_ITEMS_COUNT = 100;
    
    /**
     *
     * @var Service_Application
     */
    protected $_application = null;

    protected $_name = '';

    public function __construct($application, $name = null)
    {
        $this->_application = $application;
        if (null !== $name) {
            $this->_name = $name;
        }
    }

    public function access()
    {
        if (is_object($this->_application)) {
            switch ($this->_application->getRequestMethod()) {
                case 'GET':
                    $response = $this->_get();
                    break;
                case 'PUT':
                    $response = $this->_put();
                    break;
                case 'DELETE':
                    $response = $this->_delete();
                    break;
                case 'POST':
                    $response = $this->_post();
                    break;
                default:
                    require_once 'Service/Resource/HttpMethodNotSupportedException.php';
                    throw new Service_Resource_HttpMethodNotSupportedException();
            }
        }
        
        if (!isset($response) || empty($response)) {
            require_once 'Service/Response.php';
            $response = new Service_Response();
        }

        // @todo make 'good' resource name, like dboard.room::5
        $response->setContent('resource', $this->_name);
        
        return $response;
    }

    /**
     * @throws Service_Resource_HttpMethodNotSupported
     */
    protected function _get()
    {
        require_once 'Service/Resource/HttpMethodNotSupportedException.php';
        throw new Service_Resource_HttpMethodNotSupportedException();
    }

    /**
     * @throws Service_Resource_HttpMethodNotSupported
     */
    protected function _put()
    {
        require_once 'Service/Resource/HttpMethodNotSupportedException.php';
        throw new Service_Resource_HttpMethodNotSupportedException();
    }

    /**
     * @throws Service_Resource_HttpMethodNotSupported
     */
    protected function _delete()
    {
        require_once 'Service/Resource/HttpMethodNotSupportedException.php';
        throw new Service_Resource_HttpMethodNotSupportedException();
    }

    /**
     * @throws Service_Resource_HttpMethodNotSupported
     */
    protected function _post()
    {
        $method = strtoupper($this->_application->getParam('_method'));
        
        switch ($method) {
            case 'GET':
                return $this->_get();
            case 'PUT':
                return $this->_put();
            case 'DELETE':
                return $this->_delete();
            default:
                require_once 'Service/Resource/HttpMethodNotSupportedException.php';
                throw new Service_Resource_HttpMethodNotSupportedException();
        }
    }


}

